﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Timetable.Site.Models;
using Timetable.Site.Models.ScheduleInfo;
using Ninject;
using System.Monads;
using ScheduleInfo = Timetable.Site.Host.ScheduleInfo.ScheduleInfo;

namespace Timetable.Site.Controllers.Api
{
    public class ScheduleInfoController : BaseApiController<ScheduleInfo>
    {

        /// <summary>
        /// Return schedule info for selected department
        /// </summary>
        /// <param name="departmentId">Deparment identificator</param>
        /// <returns></returns>
        /*
        public HttpResponseMessage GetByDepartment(int departmentId)
        {
            return CreateResponse<int, IEnumerable<ScheduleInfoViewModel>>(privateGetByDepartment, departmentId);
        }
        private IEnumerable<ScheduleInfoViewModel> privateGetByDepartment(int departmentId)
        {
            
            var result = new List<ScheduleInfoViewModel>();
            var query = ScheduleInfoService.GetAllForDepartment(departmentId);

            
            query.Do((schedule) =>
            {
                var item = new ScheduleInfoViewModel
                {
                    Id = schedule.Id,
                    Lecturer = String.Format("{0} {1}. {2}.",
                        schedule.Lecturer.With(x => x.Lastname),
                        schedule.Lecturer.With(x => x.Firstname).With(x => x[0]),
                        schedule.Lecturer.With(x => x.Middlename).With(x => x[0])
                    ),
                    Faculties = schedule.Faculties.With(x => x.Name + "   "),
                    TutorialName = schedule.Tutorial.With(x => x.Name),
                    Courses = schedule.Courses.With(x => x.Id.ToString() + "   "),
                    Specialities = schedule.Specialities.With(x => x.ShortName + "   ")
                };


                switch (schedule.TutorialType.Type)
                {
                    case Types.Lecture: item.LectHoursPerWeek = schedule.HoursPerWeek.ToString();
                        item.LectGroups = schedule.Groups.Select(x => x.Code);
                        break;

                    case Types.Practice: item.PractHoursPerWeek = schedule.HoursPerWeek.ToString();
                        item.PractGroups = schedule.Groups.Select(x => x.Code);
                        break;

                    case Types.Lab: item.LabHoursPerWeek = schedule.HoursPerWeek.ToString();
                        item.LabGroups = schedule.Groups.Select(x => x.Code);
                        break;
                }

                result.Add(item);
            });

            
            foreach (var schedule in query)
            {
                var item = new ScheduleInfoViewModel
                {
                    Id = schedule.Id,
                    Lecturer = String.Format("{0} {1}. {2}.",
                        schedule.Lecturer.With(x => x.Lastname),
                        schedule.Lecturer.With(x => x.Firstname).With(x => x[0]),
                        schedule.Lecturer.With(x => x.Middlename).With(x => x[0])
                    ),
                    Faculties = schedule.Faculties.With(x => x.Name + "   "),
                    TutorialName = schedule.Tutorial.With(x => x.Name),
                    Courses = schedule.Courses.With(x => x.Id.ToString() + "   "),
                    Specialities = schedule.Specialities.With(x => x.ShortName + "   ")
                };

                switch (schedule.TutorialType.Type)
                {
                    case Types.Lecture: item.LectHoursPerWeek = schedule.HoursPerWeek.ToString();
                        item.LectGroups = schedule.Groups.Select(x => x.Code);
                        break;

                    case Types.Practice: item.PractHoursPerWeek = schedule.HoursPerWeek.ToString();
                        item.PractGroups = schedule.Groups.Select(x => x.Code);
                        break;

                    case Types.Lab: item.LabHoursPerWeek = schedule.HoursPerWeek.ToString();
                        item.LabGroups = schedule.Groups.Select(x => x.Code);
                        break;
                }

                result.Add(item);
            }

            return result;
        }*/

        /// <summary>
        /// Add new or update select schedule info
        /// </summary>
        /// <param name="model">Data model</param>
        /// <returns></returns>
       // [HttpPost]
       // public HttpResponseMessage Post(PostModel model)
       // {
        //    return CreateResponse(privatePost, model);
        //}
        //private void privatePost(PostModel model)
        //{
           // var scheduleAttr = new ScheduleInfo();

            //var auditoriums = AuditoriumService.GetByNumbers(model.AuditoriumNum.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));

            /*
            scheduleAttr.Id = model.Id;
            scheduleAttr.LikeAuditoriums = auditoriums.ToList();
            scheduleAttr.Department = model.Department;
            scheduleAttr.Faculties.Add(model.Faculty);
            scheduleAttr.HoursPerWeek = model.HoursPerWeek;
            scheduleAttr.Lecturer = model.Lecturer;
            scheduleAttr.Courses.Add(model.Course);
            scheduleAttr.Tutorial = model.Tutorial;
            scheduleAttr.TutorialType = model.TutorialType;
            scheduleAttr.Groups = model.GroupIds.Select(x => new Group { Id = x }).ToList();
            scheduleAttr.Specialities = model.SpecialityIds.Select(x => new Speciality { Id = x }).ToList();
            scheduleAttr.SubgroupCount = model.SubGroupCount;

            // Add new
            scheduleAttr
                .If(x => x.Id == 0)
                .Do((x) => DataService.Add(x));

            // Update selected
            scheduleAttr
                .If(x => x.Id != 0)
                .Do((x) => DataService.Update(x));*/
        //}

        /// <summary>
        /// Delete schedule info
        /// </summary>
        /// <param name="id">Schedule info identificator</param>
        /// <returns></returns>
        //[HttpDelete]
       // public HttpResponseMessage Delete(int id)
       // {
         //   return CreateResponse(privateDelete, id);
       // }
        //private void privateDelete(int id)
        //{
         //   if(id <= 0)
          //      throw new ArgumentException();

           // DataService.Delete(new ScheduleInfo{Id = id});
        //}

        /// <summary>
        /// Return unscheduled tutorial list
        /// </summary>
        /// <param name="facultyId"></param>
        /// <param name="courseId"></param>
        /// <param name="groupId"></param>
        /// <param name="specialityId"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetUnscheduled(int facultyId, int courseId, int groupId, int specialityId)
        {
            return CreateResponse(privateGetUnscheduled, facultyId, courseId, groupId, specialityId);
        }

        private IEnumerable<ScheduleInfo> privateGetUnscheduled(int facultyId, int courseId, int groupId, int specialityId)
        {
            if(facultyId <= 0 || courseId <= 0 || (groupId <= 0 && specialityId <= 0))
                throw new ArgumentException();

            return ScheduleInfoService.GetUnscheduled(facultyId, courseId, specialityId, groupId);
        }
    }
}
